package com.cn.wanxi.servlet.user;

import com.alibaba.fastjson.JSONObject;
import com.cn.wanxi.model.UserModel;
import com.cn.wanxi.service.IUserService;
import com.cn.wanxi.service.impl.UserServiceImpl;
import com.cn.wanxi.util.Tool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/back/user/edit")
public class UserEditServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

//        1.乱码问题

        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");

        String username = req.getParameter("username");
        String phone = req.getParameter("phone");
        String birthday = req.getParameter("birthday");
        String sex = req.getParameter("sex");
        String hobby = req.getParameter("hobby");
        String email = req.getParameter("email");
        String idString = req.getParameter("id");
        //        3.封装数据

        UserModel userModel = new UserModel();
        userModel.setUsername(username);
        userModel.setPhone(phone);
        userModel.setBirthday(birthday);
        userModel.setHobby(hobby);
        userModel.setSex(sex);
        userModel.setId(Tool.stringToInt(idString));
        userModel.setEmail(email);

        //        4.调用服务逻辑层

        IUserService iUserService = new UserServiceImpl();
//        5.得到数据

        String result = iUserService.eidt(userModel);

        //        6.返回结果
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result", result);
        resp.getWriter().println(jsonObject);
    }
}
